package com.gxuwz.endrskyweather.viewmodel;

import android.app.Application;
import android.util.Log;

import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.gxuwz.endrskyweather.model.ForecastDay;
import com.gxuwz.endrskyweather.model.Hour1dataItem;
import com.gxuwz.endrskyweather.apiclient.BaiduBceApiClient15d;
import com.gxuwz.endrskyweather.apiclient.BaiduBceApiClient24h;
import com.gxuwz.endrskyweather.apiclient.BaiduBceApiClient7d;
import com.gxuwz.endrskyweather.apiclient.IApiClient;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WeatherForecastViewModel extends AndroidViewModel {
    //private MutableLiveData<List<String>> hourlyForecast;
    private MutableLiveData<List<Hour1dataItem>> hourlyForecast;
    private MutableLiveData<List<ForecastDay>> daily7dForecast;
    private MutableLiveData<List<ForecastDay>> daily15dForecast;
    private MutableLiveData<String> hour3ForecastTip;
    private IApiClient client24h;
    private IApiClient client7d;
    private IApiClient client15d;

    private String jsonString24h;  // json 缓存(24小时预报)
    private String jsonString7d;    // json 缓存(7天预报)
    private String jsonString15d;   // json 缓存(15天预报)

    private static final String TAG = "WeatherPagerViewModel";

    public WeatherForecastViewModel(Application application) {
        super(application);
        hourlyForecast = new MutableLiveData<>();
        daily7dForecast = new MutableLiveData<>();
        daily15dForecast = new MutableLiveData<>();
        hour3ForecastTip = new MutableLiveData<>();
        client24h = new BaiduBceApiClient24h(application.getApplicationContext());
        client7d = new BaiduBceApiClient7d(application.getApplicationContext());
        client15d = new BaiduBceApiClient15d(application.getApplicationContext());
        jsonString24h = null;
        jsonString7d = null;
        jsonString15d = null;
    }

    public LiveData<List<Hour1dataItem>> getHourlyForecast() {
        return hourlyForecast;
    }

    public LiveData<List<ForecastDay>> getDaily7dForecast() {
        return daily7dForecast;
    }

    public LiveData<List<ForecastDay>> getDaily15dForecast() {
        return daily15dForecast;
    }

    public LiveData<String> getHour3ForecastTip() {
        return hour3ForecastTip;
    }

    // 加载24小时预报
    public void loadHourlyForecast(String cityCode) {
        new Thread(() -> {
            try {
                if (jsonString24h == null) jsonString24h = client24h.httpGetJsonString(cityCode);
                List<Hour1dataItem> forecast = new BaiduBceApiClient24h(jsonString24h).getHour1Forecast(cityCode);
                hourlyForecast.postValue(forecast);
                Log.d(TAG, "Hourly forecast loaded.");
                // 加载3小时预报提示，并读取显示第一个提示
                List<String> tips = client24h.getHour3Forecast(cityCode);
                hour3ForecastTip.postValue(tips.get(0));
                Log.d(TAG, "Hourly 3 forecast tips loaded.");
            } catch (Exception e) {
                Log.e(TAG, "Error loading hourly forecast: " + e.getMessage());
                hourlyForecast.postValue(null);
            }
        }).start();
    }

    // 加载7天预报
    public void loadDaily7dForecast(String cityCode) {
        new Thread(() -> {
            try {
                if (jsonString7d == null) jsonString7d = client7d.httpGetJsonString(cityCode);
                Map<String, ForecastDay> forecast = new BaiduBceApiClient7d(jsonString7d).get7DayForecast(cityCode);
                Log.d(TAG, "Forecast map size: " + forecast.size());
                // 移除今天的预报
                forecast.remove("d1");
                // 将Map转换为List
                List<ForecastDay> forecastList = new ArrayList<>(forecast.values());
                Log.d(TAG, "Forecast list size: " + forecastList.size());
                daily7dForecast.postValue(forecastList);
            } catch (Exception e) {
                Log.e(TAG, "Error loading 7-day forecast: " + e.getMessage());
                daily7dForecast.postValue(null);
            }
        }).start();
    }

    // 加载15天预报
    public void loadDaily15dForecast(String cityCode) {
        new Thread(() -> {
            try {
                if (jsonString15d == null) jsonString15d = client15d.httpGetJsonString(cityCode);
                Map<String, ForecastDay> forecast = new BaiduBceApiClient15d(jsonString15d).get15DayForecast(cityCode);
                Log.d(TAG, "15-day forecast loaded.");
                Log.d(TAG, "Forecast map size: " + forecast.size());
                // 将Map转换为List
                List<ForecastDay> forecastList = new ArrayList<>(forecast.values());
                Log.d(TAG, "Forecast list size: " + forecastList.size());
                daily15dForecast.postValue(forecastList);
            } catch (Exception e) {
                Log.e(TAG, "Error loading 15-day forecast: " + e.getMessage());
                daily15dForecast.postValue(null);
            }
        }).start();
    }

}
